<template>
  <div>
    <transition :name="transitionName">
      <router-view></router-view>
    </transition>

    <!-- <h4>Vuex里的num是{{ $store.state.num }}</h4>
    <button @click="increment">点击+1</button> -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      transitionName: ''
    }
  },
  // created() {
  //   console.log(this);
  // },
  // methods: {
  //   increment() {
  //     //! 不推荐
  //     // this.$store.state.num+= 1
  //     // console.log(this);

  //     this.$store.commit("SET_NUM", 3);
  //   },
  // },

  watch: {
    //使用watch 监听$router的变化
    $route(to, from) {
      //如果to索引大于from索引,判断为前进状态,反之则为后退状态
      console.log(to, 'to')
      console.log(from, 'from')
      if (to.meta.index > from.meta.index) {
        //设置动画名称
        this.transitionName = 'slide-left'
      } else {
        this.transitionName = 'slide-right'
      }
    }
  }
}
</script>

<style lang="less" scoped>
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  will-change: transform;
  transition: all 500ms;
  position: absolute;
}
// 右滑初始装状态
.slide-right-enter {
  // opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

// 右滑执行状态
.slide-right-leave-active {
  // opacity: 0;
  transform: translate3d(100%, 0, 0);
}
// 右滑最终状态
.slide-right-enter-to {
  // opacity: 1;
  transform: translate3d(0, 0, 0);
}
// 左滑初始装状态
.slide-left-enter {
  // opacity: 0;
  transform: translate3d(100%, 0, 0);
}
// 左滑执行状态
.slide-left-leave-active {
  // opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
// 左滑最终状态
.slide-right-enter-to {
  // opacity: 1;
  transform: translate3d(0, 0, 0);
}
</style>
